package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.domain.YyStudent;
import com.ruoyi.system.service.impl.YyStudentServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.YyPatriarch;
import com.ruoyi.system.service.IYyPatriarchService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 家长Controller
 *
 * @author ruoyi
 * @date 2024-05-17
 */
@Controller
@RequestMapping("/system/patriarch")
public class YyPatriarchController extends BaseController
{
    private String prefix = "system/patriarch";

    @Autowired
    private IYyPatriarchService yyPatriarchService;

    @Autowired
    private YyStudentServiceImpl yyStudentService;

    @Autowired
    private SysPasswordService passwordService;

    @RequiresPermissions("system:patriarch:view")
    @GetMapping()
    public String patriarch()
    {
        return prefix + "/patriarch";
    }

    /**
     * 查询家长列表
     */
    @RequiresPermissions("system:patriarch:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YyPatriarch yyPatriarch)
    {
        startPage();
        List<YyPatriarch> list = yyPatriarchService.selectYyPatriarchList(yyPatriarch);
        return getDataTable(list);
    }

    /**
     * 导出家长列表
     */
    @RequiresPermissions("system:patriarch:export")
    @Log(title = "家长", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YyPatriarch yyPatriarch)
    {
        List<YyPatriarch> list = yyPatriarchService.selectYyPatriarchList(yyPatriarch);
        ExcelUtil<YyPatriarch> util = new ExcelUtil<YyPatriarch>(YyPatriarch.class);
        return util.exportExcel(list, "家长数据");
    }

    /**
     * 新增家长
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        YyStudent student = new YyStudent();
        modelMap.put("stu", yyStudentService.selectYyStudentList(student));
        return prefix + "/add";
    }

    /**
     * 新增保存家长
     */
    @RequiresPermissions("system:patriarch:add")
    @Log(title = "家长", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YyPatriarch yyPatriarch)
    {

        //新增系统用户
        SysUser sysUser = new SysUser();
        sysUser.setUserName(yyPatriarch.getPatriarchName());
        sysUser.setLoginName(yyPatriarch.getpAccount());
        sysUser.setSalt(ShiroUtils.randomSalt());
        sysUser.setPassword(passwordService.encryptPassword(sysUser.getLoginName(), yyPatriarch.getpPassword(), sysUser.getSalt()));


        return toAjax(yyPatriarchService.insertYyPatriarch(yyPatriarch,sysUser));
    }

    /**
     * 修改家长
     */
    @RequiresPermissions("system:patriarch:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        YyPatriarch yyPatriarch = yyPatriarchService.selectYyPatriarchById(id);
        mmap.put("yyPatriarch", yyPatriarch);
        mmap.put("stu", yyStudentService.selectYyStudentByIdList(Long.parseLong(String.valueOf(yyPatriarch.getStuId()))));
        return prefix + "/edit";
    }

    /**
     * 修改保存家长
     */
    @RequiresPermissions("system:patriarch:edit")
    @Log(title = "家长", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YyPatriarch yyPatriarch)
    {
        return toAjax(yyPatriarchService.updateYyPatriarch(yyPatriarch));
    }

    /**
     * 删除家长
     */
    @RequiresPermissions("system:patriarch:remove")
    @Log(title = "家长", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(yyPatriarchService.deleteYyPatriarchByIds(ids));
    }
}
